package com.hisan.waimaidao.carvas;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Process;
import android.support.annotation.NonNull;
import android.support.annotation.StyleRes;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.hisan.waimaidao.myview.Progress;


/**
 * 创建时间 : 2017/9/21
 * 创建人：yangyingqi
 * 公司：嘉善和盛网络有限公司
 * 备注：自定义加载条的提示框
 */
public class ProgressDialog extends Dialog {

    public ProgressDialog(@NonNull Context context) {
        super(context);}
    public ProgressDialog(@NonNull Context context, @StyleRes int themeResId) {
        super(context, themeResId);}

    /**
     * 建造者类
     *
     * @author Administrator
     */
    public static class Builder {
        private View view;
        private Activity context;
        private Progress progress;

        public Progress getProgress() {
            return progress;
        }

        public Builder setContentView(View v) {
            this.view = v;
            return this;
        }


        public Builder(Activity context) {
            this.context = context;
        }
        /**
         * 创建一个AlertDialog
         *
         * @return
         */
        public ProgressDialog create() {
            LayoutInflater inflater = LayoutInflater.from(context);
            final ProgressDialog dialog = new ProgressDialog(context,
                    R.style.DialogStyle);
            //取消外部点击消失
            dialog.setCanceledOnTouchOutside(false);
            //取消物理返回点击消失
            dialog.setCancelable(true);
            View layout = null;
            if (null != view) {
                layout = view;
            } else {
                layout = inflater.inflate(R.layout.progress_item, null);
                progress=layout.findViewById(R.id.progress);
            }
            dialog.setOnDismissListener(new OnDismissListener() {
                @Override
                public void onDismiss(DialogInterface dialogInterface) {
                    if (progress!=null){
                        progress.revomehandler();
                    }
                }
            });
            // 设置对话框的视图
            ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT);
            dialog.setContentView(layout, params);
            return  dialog;
        }
    }

}
